package com.lanyou.esb.web.quantityControlcontrol;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lanyou.cook.common.Constants;
import com.lanyou.cook.service.security.UserSessionService;
import com.lanyou.esb.datadictionary.EnableType;
import com.lanyou.esb.service.QuantityDetailService;
import com.lanyou.esb.vo.QuantityDetailVo;
import com.lanyou.esb.web.param.QuantityControlParam;
import com.lanyou.esb.web.serviceLogManagement.vo.AccessLogVo;

/**
 * 流量明细报表控制器
 * 
 * @author Davey.wu
 */
@RestController
@RequestMapping(Constants.BASE_URL_API + "/quantityDetailReport")
public class QuantityDetailReportController {
	@Autowired
	QuantityDetailService quantityDetailService;

	@Autowired
	UserSessionService userSessionService;

	/**
	 * 查找流量明细信息
	 * 
	 * @param param
	 * @param pageable
	 * @return
	 */
	@RequestMapping("/findQuantityDetailReport")
	public Page<QuantityDetailVo.Entry> findQuantityDetailReport(
			QuantityControlParam param, Pageable pageable) {
		// 获取当前用户可访问的系统标识
		Collection<String> collection = userSessionService
				.findFieldSystemIdsOfCurrentAuthenticatedUserCanManage();
		// 没有管理任务系统，返回空集
		if (collection == null || collection.isEmpty()) {
			return new PageImpl<QuantityDetailVo.Entry>(
					new ArrayList<QuantityDetailVo.Entry>(0), pageable, 0);
		}
		List<String> sysIds = Arrays.asList(collection.toArray(new String[0]));

		if (StringUtils.isNotBlank(param.getIfName())) {
			param.setIfName("%" + param.getIfName() + "%");
		}
		// 只统计可用的接口
		param.setIsEnable(EnableType.ENABLE.ValueCode);
		Page<QuantityDetailVo.Entry> page = null;
		try {
			// 查询数据
			QuantityDetailVo vo = quantityDetailService
					.findQuantityDetailByMonth(param, pageable, sysIds);
			if (vo != null) {
				page = new PageImpl<QuantityDetailVo.Entry>(vo.getEntrys(),
						pageable, vo.getTotal());
			}
		} catch (Exception e) {
			page = new PageImpl<QuantityDetailVo.Entry>(new ArrayList<>(0),
					pageable, 0);
		}
		return page;
	}
}
